//
//  STSearchViewController.m
//  赛拓
//
//  Created by 王磊 on 8/13/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STSearchViewController.h"
#import "STCustomTF.h"
@interface STSearchViewController ()<STBaseViewDelegate>
@property (nonatomic ,strong) STCustomTF* search;
@end

@implementation STSearchViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.search becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.view endEditing:true];
}

- (void)initSubviews {
    [super initSubviews];
    
    [self initNavi];
}
- (void)initNavi {
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemRightOffsetWithTarget:self action:@selector(searchTriggered) title:@"搜索" highTitle:@"搜索" ];
    
    STCustomTF *tf = [STCustomTF instance];
    
    tf.frame = CGRectMake(100 * ST_RATE, 0, ST_SCREEN_WIDTH - 100 * ST_RATE * 2, 40);
    
    self.search = tf;
    
    tf.backgroundColor = [UIColor redColor];
    
    self.navigationItem.titleView = tf;
    
    tf.maxLength = 15;
    
    tf.returnKeyType = UIReturnKeySearch;
    
    tf.mDelegate = self;
}

- (void)searchTriggered {
    [self.search resignFirstResponder];
    
    [self onItemClick:STItemClickTypeSearch withData:nil forIndexPath:nil];
    
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    switch (itemType) {
        case STItemClickTypeSearch:
        {
            
            [self.navigationController popViewControllerAnimated:true];
        }
            break;
        case STItemClickTypeReturnKey:
        {
            [self searchTriggered];
        }
            break;
        default:
            break;
    }
}
@end
